const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin")
module.exports = {
    entry: path.resolve(__dirname, "../src/index.js"),
    output: {
        path: path.resolve(__dirname, "../build"),
        filename: "./js/index.js",
        publicPath: "/"
    },
    module: {
        rules: [
            //     {
            //     test: /\.html$/i,
            //     use: [
            //         'html-loader'
            //     ]
            // },
            {
                test: /\.ejs$/i,
                loader: "ejs-loader",
                options: {
                    variable: 'data', // 可以在模块当中使用data进行数据处理
                }
            },
            {
                test: /\.css$/i,
                use: [
                    //use中loader加载是有顺序的，先下后上，注意有的loader需要按照顺序书写
                    "style-loader",
                    'css-loader'
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: path.resolve(__dirname, "../public/index.html"),
            minify: {
                collapseWhitespace: true,
                keepClosingSlash: true,
                removeComments: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true,
                useShortDoctype: true
            }
        }),
        new CopyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, "../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                },
            }],
        }),

    ],

    //解析
    resolve: {
        //配置路径别名
        alias: {
            $v: path.resolve(__dirname, "../src/views"),
            $r: path.resolve(__dirname, "../src/routes"),
            $a: path.resolve(__dirname, "../src/api"),
        },
        //配置扩展 尝试按顺序解析这些后缀名。如果有多个文件有相同的名字，但后缀名不同，webpack 会解析列在数组首位的后缀的文件 并跳过其余的后缀。
        extensions: [".js", ".json", ".ejs"],
    },
    mode: "production"
}